<template>
  <div class="sidebar">
    <el-menu class="sidebar-el-menu" :default-active="onRoutes" :collapse="collapse" background-color="#324157" text-color="#bfcbd9" active-text-color="#20a0ff" unique-opened router>
      <template v-for="item in items">
        <template v-if="item.children">
          <el-submenu :index="item.page" :key="item.id">
            <template slot="title">
              <i :class="(item.icon)? item.icon:'el-icon-setting'"></i><span slot="title">{{item.title}}</span>
            </template>
            <template v-for="(subItem, i) in item.children">
              <template v-if="subItem.children">
                <el-submenu :index="subItem.page" :key="subItem.id">
                  <template slot="title">
                  <i :class="(subItem.icon)? subItem.icon:'el-icon-setting'"></i><span slot="title">{{subItem.title}}</span>
                  </template>
                    <el-menu-item v-for="(sub1Item, i) in subItem.children" :key="sub1Item.id" :index="sub1Item.page">
                    {{ sub1Item.title }}
                    </el-menu-item>
                </el-submenu>
              </template>
              <template v-else>
                <el-menu-item :index="subItem.page" :key="subItem.id">
                  <i :class="(subItem.icon)? subItem.icon:'el-icon-setting'"></i><span slot="title">{{subItem.title}}</span>
                </el-menu-item>
              </template>
            </template>
          </el-submenu>
        </template>
        <template v-else>
          <el-menu-item :index="item.page" :key="item.id">
            <i :class="(item.icon)? item.icon:'el-icon-setting'"></i><span slot="title">{{item.title}}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script>
import bus from "../common/bus";
import {request} from "@/libs/core.js"
import {mapState} from 'vuex' //本地取信息
export default {
  data() {
    return {
      collapse: false,
    };
  },
  computed: {
    onRoutes() {
      return this.$route.path.replace("/", "");
    },
    ...mapState({
        items: (state) => {
          return state.adminSrv.module;
        }
      })
  },
  created() {
    bus.$on("collapse", (msg) => {
      this.collapse = msg;
    });
    this.$store.dispatch("adminSrv/loadModule");
  }
};
</script>

<style scoped>
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 70px;
  bottom: 0;
  overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
  width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
  width: 250px;
}
.sidebar > ul {
  height: 100%;
}
</style>
